package com.liu.dao.impl;

import com.liu.dao.IReadLogDao;
import com.liu.dto.ReadRecordDTO;
import com.liu.util.LoggerFactory;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/12 21:13
 * @Description
 */
public class ReadLogDaoImpl extends GenericDaoImpl<ReadRecordDTO> implements IReadLogDao, LoggerFactory {

    public List<ReadRecordDTO> queryByUser_id(Integer user_id) throws SQLException {

        String sql = //
                "select " + //
                    "cate_name, " + //
                    "author_name, " + //
                    "last_time " + //
                "from readlog r " + //
                "join novel n " + //
                "on r.novel_id = n.novel_id " + //
                "join category c " + //
                "on n.cate_id = c.sort " + //
                "join `user` " + //
                "on n.user_id = `user`.user_id " + //
                "where `user`.user_id = " + user_id;

        return jdbcTemplate.query(sql, resultSet -> {

            List<ReadRecordDTO> list = new ArrayList<>();

            while (resultSet.next()) {
                ReadRecordDTO o = new ReadRecordDTO();
                o.setCate_name(resultSet.getString("cate_name"));
                o.setAuthor_name(resultSet.getString("author_name"));
                o.setLast_time(resultSet.getTimestamp("last_time"));

                list.add(o);
            }

            return list;
        });
    }

    public Integer deleteByAuthor_name(String author_name) throws SQLException {

        String sql = //
                "delete from favorite where author_name = ?";

        return jdbcTemplate.updateOrInsert(sql, //
                new Object[]{
                        author_name
                },
                new Integer[]{
                        Types.VARCHAR,
                });
    }

    @Override
    public Integer save(ReadRecordDTO readRecordDTO) throws SQLException {
        return null;
    }

    @Override
    public Integer update(ReadRecordDTO readRecordDTO) throws SQLException {
        return null;
    }

    @Override
    public List<ReadRecordDTO> query(String where) throws SQLException {
        return null;
    }

    @Override
    public List<ReadRecordDTO> queryAll() throws SQLException {
        return null;
    }

    @Override
    public ReadRecordDTO querySingle(Integer id) throws SQLException {
        return null;
    }

    @Override
    public Integer queryCount(String table, String where) throws SQLException {
        return null;
    }
}
